package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.ExceptionInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalDcPublicConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * 订单信息模块
 */
@Service
public class ExceptionInfoModuleHandler extends AbstractModuleHandler {

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        return getExceptionInfo(context);
    }

    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        return getExceptionInfo(context);
    }

    public List<? extends ModuleVO> getExceptionInfo(AcceptContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        Map<String, Object> exceptionInfo = querySceneInstService.getExtParameterValue(context.getAcceptRequest().getSceneInstId(), LocalKeyConsts.SCENE_EXT_PARAM_EXCEPTION_INFO);
        if (Objects.isNull(exceptionInfo) || exceptionInfo.isEmpty()) {
            return Lists.newArrayList();
        }
        ExceptionInfoVO exceptionInfoVo = new ExceptionInfoVO();
        //{"exceptionType":"NOT_APPROVED","dealUserId":"8000001100","originTaskName":"资料完整性审核","dealDate":"2020-09-16"}
        //{"exceptionType":"SYS_EXCEPTION","originTaskName":"一站式业务开通","dealDate":"2020-09-11","errorCode":"9001","failReason":"开通环节,合同信息不可为空!"}
        List<InstAttrDTO> instAttrDTOList = Lists.newArrayList();
        List<DcPublic> expAttrConfigs = DcPublicCache.get(LocalDcPublicConsts.DC_2020092001);
        if (KiteListUtils.isEmpty(expAttrConfigs)) {
            expAttrConfigs = Lists.newArrayList();
        }
        expAttrConfigs.forEach(attrConfig -> {
            InstAttrDTO attr = new InstAttrDTO();
            attr.setFieldName(attrConfig.getPkey());
            attr.setCname(Optional.ofNullable(attrConfig).map(DcPublic::getPname).orElse(""));
            attr.setAttrValueTypeId("TT");
            if (exceptionInfo.containsKey(attrConfig.getPkey())) {
                String value = (String) exceptionInfo.get(attrConfig.getPkey());
                attr.setValue(value);
                attr.setValueDesc(value);
            }
            else {
                attr.setValue("");
                attr.setValueDesc("");
            }
            instAttrDTOList.add(attr);
        });

        exceptionInfoVo.setCustInfoAttr(instAttrDTOList);
        exceptionInfoVo.setOfferInstId(context.getAcceptRequest().getSceneInstId());
        
        String exceptionType = (String) exceptionInfo.get("exceptionType");
        if ("SYS_EXCEPTION".equals(exceptionType)) {
            exceptionInfoVo.setModuleName("异常信息");
        }
        exceptionInfoVo.setLayoutPage("CustInfo");
        exceptionInfoVo.setIsEdit("F");
        
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setExceptionInfo(exceptionInfoVo);
        return Collections.singletonList(exceptionInfoVo);
    }
}
